package com.baosight.sczx.zm.ch.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cg.DailyReport;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.ch.domain.ZMCHWorkReport;

/**
 * <p>Title: ServiceZMCH80.java</p>
 * <p>Description: 班组工作汇报</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-10-25 下午12:14:15  张小飞  Create
 *</pre>
 */
public class ServiceZMCH80 extends ServiceEPBase {
	
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p>
	 * <p>return value: ServiceZMCH80</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午03:26:45 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p>
	 * <p>return value: ServiceZMCH80</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午03:26:59 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdMiningId = (String)UserSession.getInSessionProperty("miningCode");
		String fdDepartmentId = UserSession.getInSessionProperty("orgCode").toString();//部门ID
	
		StaticTools.setQueryParameters(inInfo, "fdDepartmentId", fdDepartmentId, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);//添加查询条件
		
		EiInfo outInfo = super.query(inInfo, "ZMCHWorkReport.query", new ZMCHWorkReport());
		
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum",fdMiningId, true));//班次
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.ch.status", new String[]{"草拟", "上报"}, new String[]{"草拟", "上报"}, true));//状态
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", true, fdMiningId));
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.ch.workPlanId", true));//工作安排
		
		return outInfo;
	}	
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p>
	 * <p>return value: ServiceZMCH80</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午03:31:06 张小飞
	 * </p>
	 *
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out = new EiInfo(); 

		ZMCHWorkReport _dom = new ZMCHWorkReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0 ; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
//			boolean validate = this.validate(inInfo, _dom.getFdCurrentDate(), _dom.getFdDepartmentId(), _dom.getFdWorknumId(), null);
//			if(validate) {
//				out = this.query(inInfo);
//				return out;
//			}
			
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CH_WORKREPORT"));
			_dom.setFdDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
			_dom.setFdDepartment(UserSession.getInSessionProperty("orgName").toString());
			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());
			_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(), _dom.getFdWorknumId()));
			
			dao.insert("ZMCHWorkReport.insert", _dom);
		}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p>
	 * <p>return value: ServiceZMCH80</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午04:59:23 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {		
		EiInfo out=new EiInfo();
		ZMCHWorkReport _dom = new ZMCHWorkReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			boolean validate = this.validate(inInfo, _dom.getFdCurrentDate(), _dom.getFdDepartmentId(), _dom.getFdWorknumId(), _dom.getFdKeyId());
			if(validate) {
				out = this.query(inInfo);
				return out;
			}
			
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(), _dom.getFdWorknumId()));
			
		    this.dao.update("ZMCHWorkReport.update", _dom);			
		}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
	}	
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p>
	 * <p>return value: ServiceZMCH80</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午04:12:10 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings("unused")
	@Override
	public EiInfo delete(EiInfo inInfo) {
        
		EiInfo outInfo = null;
		
		List<String> _list = new ArrayList<String>();
		Map<String,List<String>> params = new HashMap<String,List<String>>();

		for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{

			EiInfo _temp = super.delete(inInfo, "ZMCHWorkReport.delete");
	        outInfo = this.query(inInfo);
	        outInfo.setMsg(_temp.getMsg());
	        outInfo.setDetailMsg(_temp.getDetailMsg());
		}
		
		return outInfo;
	}	
	
	/**
	 * 上报
	 * <p>Function: report</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午04:07:27 张志芳
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCHWorkReport _dom = new ZMCHWorkReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> map = new HashMap<String, String>();
			map.put("fdCurrentDate", _dom.getFdCurrentDate());
			map.put("fdDepartmentId", _dom.getFdDepartmentId());
			map.put("fdWorknumId", _dom.getFdWorknumId());
			
			boolean validate = DailyReport.validate("ZMCGWorkReportFinal.query", map);
			if(validate) {
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("已经存在数据，无需上报！");
				return out; 
			}
			else {
				int result = dao.update("ZMCHWorkReport.report", _dom);
				if(result > 0){
					if(_dom.getFdDepartmentId() != null) {
						String fdDepartmentId = _dom.getFdDepartmentId().substring(0, 9);
						_dom.setFdDepartmentId(fdDepartmentId);
						_dom.setFdDepartment(Department.getInstance().getDepartmentNameById(fdDepartmentId));
					}

					DailyReport.copy("ZMCGWorkReportFinal.insert", _dom.toMap());
			}}
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	
	}
	
	/**
	 * 验证是否存在同一天、相同班次的记录
	 * @param inInfo
	 * @param date
	 * @param fdDepartmentId
	 * @param fdWorknumId
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String fdCurrentDate, String fdDepartmentId, String fdWorknumId, String updateKeyId) {
		
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("fdDepartmentId", fdDepartmentId);
		_map.put("fdCurrentDate", fdCurrentDate);
		_map.put("fdWorknumId", fdWorknumId);
		_map.put("updateKeyId", updateKeyId);
		
		List result = dao.query("ZMCHWorkReport.query", _map);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "每个部门每天每班次只能存在一条记录！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}
}
